package sg.edu.nus.iss.vmcs.customer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import sg.edu.nus.iss.vmcs.util.MessageDialog;
import sg.edu.nus.iss.vmcs.util.VMCSException;

/**
* This class represents the control object that implements the coin denomination
* selection (button presses by the customer) on the Customer Panel when coins are
* entered for a drink to be dispensed. Action will be performed in corresponding to
* the button pressed
*
*/

public class CoinInputListener implements ActionListener {

	private  CoinReceiver coinReceive;

/**
 * This constructor creates an instance of the Coin Input Listener.
 * It receives reference for CoinReceiver as a parameter.
 * @param cr CoinReceiver
 * @see CoinReceiver
 */	
	public CoinInputListener(CoinReceiver cr) {
		coinReceive = cr;
	}

/**
 * Invoked when an action occurs. Implemented as a part of interface 
 * @param e ActionEvent 
 * @see CoinReceiver#receiveCoin(double)
 */	
	public void actionPerformed(ActionEvent e) {

		String cmd;
		double weight;
		Button btn;

		btn = (Button) e.getSource();
		cmd = btn.getActionCommand();
		weight = Double.parseDouble(cmd);
		
		try {
			coinReceive.receiveCoin(weight);
		} catch (VMCSException e1) {
			MessageDialog msg = new MessageDialog(coinReceive.getTransactionController().getCustomerPanel(), e1.getMessage());
			// TODO Auto-generated catch block
			//e1.printStackTrace();
		}
	}
}
